/*
 * ZipUtils.java
 *
 * Created on December 7, 2006, 11:57 AM
 *
 */

package com;
/*
 * User: Wesly Billman
 * Date: Nov 13, 2006
 * Time: 2:20:19 PM
 */

import java.io.*;
import java.util.zip.*;
import java.util.Enumeration;

public class ZipUtils
{
    // specify buffer size for extraction
    static final int BUFFER = 2048;


    /**
     *  Unzips a file to the specified output directory.
     * @param input_zip_file
     * @param output_dir
     */
    public static void unzipFile(String input_zip_file, String output_dir)
    {
        try
        {
            System.out.println("Unzipping: " + input_zip_file);
            System.out.println("To Directory: " + output_dir);

            // Specify file to decompress
            String inFileName = input_zip_file;
            // Specify destination where file will be unzipped
            String destinationDirectory = output_dir;

            File sourceZipFile = new File(inFileName);
            File unzipDestinationDirectory = new File(destinationDirectory);

            // Open Zip file for reading
            ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);

            // Create an enumeration of the entries in the zip file
            Enumeration zipFileEntries = zipFile.entries();

            // Process each entry
            while (zipFileEntries.hasMoreElements())
            {
                // grab a zip file entry
                ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();

                String currentEntry = entry.getName();
                System.out.println("Extracting: " + entry);

                File destFile =
                        new File(unzipDestinationDirectory, currentEntry);

                // grab file's parent directory structure
                File destinationParent = destFile.getParentFile();

                // create the parent directory structure if needed
                destinationParent.mkdirs();

                // extract file if not a directory
                if (!entry.isDirectory())
                {
                    BufferedInputStream is =
                            new BufferedInputStream(zipFile.getInputStream(entry));
                    int currentByte;
                    // establish buffer for writing file
                    byte data[] = new byte[BUFFER];

                    // write the current file to disk
                    FileOutputStream fos = new FileOutputStream(destFile);
                    BufferedOutputStream dest =
                            new BufferedOutputStream(fos, BUFFER);

                    // read and write until last byte is encountered
                    while ((currentByte = is.read(data, 0, BUFFER)) != -1)
                    {
                        dest.write(data, 0, currentByte);
                    }
                    dest.flush();
                    dest.close();
                    is.close();
                }
            }
            zipFile.close();
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
    }

    public static void main (String argv[])
    {
        File inFile = new File("C:\\Ace2.zip");
        File outFile = new File("C:\\Ace2Out.zip");

        unzipFile("C:\\Ace2.zip", "C:\\temp\\");
    }
}

